
clear
set seed 1234
local cutoff=0.01									// where to truncate reliability (0 or 0.01)
capture log close
capture file close myfile
log using ".\results\maintablesfigs\tabs\Table2_coeftest.txt", text append
clear
save ".\data\temp\temp1", replace emptyok

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"

qui reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0  , cluster(rt_id)
local beta=_b[_cons]
local se=_se[_cons]
 
use ".\preparation\acct_by_acct_noMP5_wvor (1)",clear
drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.



sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum VOT, detail
local VOT=round(`r(mean)',0.01)
local VOT25=round(`r(p25)',0.01)
local VOT75=round(`r(p75)',0.01)
sum VOR, detail
local VOR=round(`r(mean)',0.01)
local VOR25=round(`r(p25)',0.01)
local VOR75=round(`r(p75)',0.01)



* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen pstat=.
save ".\data\temp\CI95_noMP5_wvor_test", replace emptyok
*relMP rel
	forvalues i=1(1)500 {
		quietly{
			use ".\data\temp\temp3", clear
			gen temp1=rnormal(urgency,urgencyse)
			sum temp1 
			local h=(`r(mean)'-`beta')^2/(`r(sd)'^2-`se'^2)				// single coefficient hausman statistic
			local chi=chi2(1,`h')										// ~chi2 1 df
			use ".\data\temp\CI95_noMP5_wvor_test", clear
			replace pstat=`chi' if _n==`i'
			save ".\data\temp\CI95_noMP5_wvor_test", replace
			}
			di ".", _continue
		}
	
sum pstat
